//
//  WBPicCollectionView.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/17.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBPicCollectionView.h"
#import "WBPicCollectionCell.h"
#import "WBPicUrlModel.h"

#import "SDPhotoBrowser.h"

@interface WBPicCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate,SDPhotoBrowserDelegate>

@end

static NSString *cell_ID = @"WBPicCollectionCell_ID";
@implementation WBPicCollectionView


-(void)awakeFromNib{

    [super awakeFromNib];
    
    self.dataSource = self;
    self.delegate = self;
    
    UINib *nib = [UINib nibWithNibName:@"WBPicCollectionCell" bundle:nil];
    [self registerNib:nib forCellWithReuseIdentifier:cell_ID];
    
}

-(void)setPicUrls:(NSArray<WBPicUrlModel *> *)picUrls{
 
    _picUrls = picUrls;
    //重新刷新数据
    [self reloadData];
}

#pragma mark : - UICollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.picUrls.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    WBPicCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cell_ID forIndexPath:indexPath];
    
    cell.thumbnail_pic = self.picUrls[indexPath.item].wap360;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    /*
     @property (nonatomic, weak) UIView *sourceImagesContainerView;
     @property (nonatomic, assign) NSInteger currentImageIndex;
     @property (nonatomic, assign) NSInteger imageCount;
     
     @property (nonatomic, weak) id<SDPhotoBrowserDelegate> delegate;
     
     - (void)show;
     */
    
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc]init];
    
    browser.sourceImagesContainerView = self;
    browser.currentImageIndex = indexPath.item;
    browser.imageCount = self.picUrls.count;
    browser.delegate = self;
    
    [browser show];
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    WBPicCollectionCell *cell = (WBPicCollectionCell *)[self cellForItemAtIndexPath:indexPath];
    
    return cell.picture.image;
}

-(NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index{

    NSString *str = self.picUrls[index].bimiddleImage;
    return [NSURL URLWithString:str];
}

@end




